package com.topboya.linker.ui.presenter;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.topboya.linker.AppConst;
import com.topboya.linker.R;
import com.topboya.linker.api.ApiRetrofit;
import com.topboya.linker.exception.ServerException;
import com.topboya.linker.ui.activity.CustomerActivity;
import com.topboya.linker.ui.activity.MainActivity;
import com.topboya.linker.ui.activity.xMainActivity;
import com.topboya.linker.ui.base.Presenter.BasePresenter;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.ui.view.ILoginAtView;
import com.topboya.linker.utils.CacheUtils;
import com.topboya.linker.utils.LogUtils;
import com.topboya.linker.utils.UIUtils;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class LoginAtPresenter extends BasePresenter<ILoginAtView> {

    private static final String TAG = "LoginAtPresenter-";

    public LoginAtPresenter(BaseActivity context) {
        super(context);
    }

    public  ImageView img_code ;
    public void login(int loginCount) {

        String phone = getView().getEtPhone().getText().toString().trim();
        String pwd = getView().getEtPwd().getText().toString().trim();
//        String phone = "13901018066";
//        String phone = "13911554303";
//        String pwd = "111";
        String code = getView().getEtCode().getText().toString().trim();
        img_code = getView().getImgCode();
        img_code.setOnClickListener(v -> {

            String url = AppConst.LOGIN_CODE_URL + "?loginName=" + phone;
            Glide.with(mContext).load(url).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).into(img_code);
        });
        LogUtils.v(TAG+"phone:"+phone + "pwd:"+pwd+"code:"+code);
        if (TextUtils.isEmpty(phone)) {
            UIUtils.showToast(UIUtils.getString(R.string.phone_not_empty));
            return;
        }
        if (TextUtils.isEmpty(pwd)) {
            UIUtils.showToast(UIUtils.getString(R.string.password_not_empty));
            return;
        }
        mContext.showWaitingDialog(UIUtils.getString(R.string.please_wait));

        if (loginCount >3){

            RelativeLayout rl_login_code = getView().getLinearCode();
            rl_login_code.setVisibility(View.VISIBLE);
            ImageView img_code = getView().getImgCode();
            String url = AppConst.LOGIN_CODE_URL + "?loginName=" + phone;
            Glide.with(mContext).load(url).into(img_code);
            mContext.hideWaitingDialog();

        }
        ApiRetrofit.getInstance().authorize(phone, pwd, "m", code)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(loginResponse -> {
                    String success = loginResponse.getMeta().getSuccess();
                    String message = loginResponse.getMeta().getMessage();
                    String coderesult = loginResponse.getMeta().getCode();
                    mContext.hideWaitingDialog();
                    LogUtils.v(TAG + "coderesult1:"+coderesult);

                    if (null != success && Boolean.parseBoolean(success)) {
//                        UserCache.save(loginResponse.getData().getUser().getId(), phone, loginResponse.getResult().getToken());
                        String token = loginResponse.getData().getToken();
                        AppConst.TOKEN = token;
                        AppConst.USERID =loginResponse.getData().getUser().getId();
                        AppConst.OFFICEID = loginResponse.getData().getUser().getOfficeId();
                        mContext.jumpToActivityAndClearTask(xMainActivity.class);
                        CacheUtils.putLoginname(mContext,AppConst.PHONE,phone);
                        CacheUtils.putLoginpw(mContext,AppConst.PW,pwd);
//                        mContext.finish();
                    } else if (coderesult.equals("2000001") ||loginCount >3) {

                        RelativeLayout rl_login_code = getView().getLinearCode();
                        rl_login_code.setVisibility(View.VISIBLE);
                        String url = AppConst.LOGIN_CODE_URL + "?loginName=" + phone;
                        Glide.with(mContext).load(url).diskCacheStrategy(DiskCacheStrategy.NONE).into(img_code);
                        LogUtils.v(TAG + "coderesult:"+coderesult);
                        UIUtils.showToast(message);

                    } else {

//                        RelativeLayout rl_login_code = getView().getLinearCode();
//                        rl_login_code.setVisibility(View.VISIBLE);
//                        String url = AppConst.LOGIN_CODE_URL + "?loginName=" + phone;
//                        Glide.with(mContext).load(url).diskCacheStrategy(DiskCacheStrategy.NONE).into(img_code);
//                        loginError(new ServerException(UIUtils.getString(R.string.login_error) + message));
                        LogUtils.v(TAG + "message:"+message);
                        UIUtils.showToast(message);
                    }
                }, this::loginError);
    }

    private void loginError(Throwable throwable) {

        LogUtils.v(throwable.getLocalizedMessage());
//        UIUtils.showToast(throwable.getLocalizedMessage());
        LogUtils.v(TAG + "loginError:" + throwable.getLocalizedMessage());
        mContext.hideWaitingDialog();
    }

}
